package com.jmb.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jmb.api.common.exception.LeaseException;
import com.jmb.api.common.result.ResultCodeEnum;
import com.jmb.api.model.entity.Permission;
import com.jmb.api.service.PermissionService;
import com.jmb.api.mapper.PermissionMapper;
import com.jmb.api.vo.permission.PermissionInfoVo;
import com.jmb.api.vo.permission.PermissionUpdateVo;
import com.jmb.api.vo.permission.PermissionVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author jinbo
* @description 针对表【permission】的数据库操作Service实现
* @createDate 2025-04-08 13:53:56
*/
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission>
    implements PermissionService{

    @Autowired
    private PermissionMapper permissionMapper;
    @Override
    public void savePermission(PermissionVo permissionVo) {
        if (permissionVo ==null){
            throw new LeaseException(ResultCodeEnum.WEB_API_parameter);
        }
        if (StringUtils.isBlank(permissionVo.getPermissionCode()) && StringUtils.isBlank(permissionVo.getPermissionName())){
            throw new LeaseException(ResultCodeEnum.WEB_API_parameter);
        }

        LambdaQueryWrapper<Permission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Permission::getPermissionCode,permissionVo.getPermissionCode());
        if (this.count(queryWrapper)>0){
            throw new LeaseException(ResultCodeEnum.WEB_API_PERMISSION_CODE);
        }
        Permission permission = new Permission();
        BeanUtils.copyProperties(permissionVo,permission);
        this.save(permission);
    }

    @Override
    public void updatePermission(PermissionUpdateVo permissionUpdateVo) {
        if (permissionUpdateVo ==null){
            throw new LeaseException(ResultCodeEnum.WEB_API_parameter);
        }
        if (StringUtils.isBlank(permissionUpdateVo.getPermissionCode()) && StringUtils.isBlank(permissionUpdateVo.getPermissionName())){
            throw new LeaseException(ResultCodeEnum.WEB_API_parameter);
        }
        LambdaQueryWrapper<Permission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Permission::getPermissionCode,permissionUpdateVo.getPermissionCode()).ne(Permission::getId,permissionUpdateVo.getId());
        if (this.count(queryWrapper)>0){
            throw new LeaseException(ResultCodeEnum.WEB_API_PERMISSION_CODE);
        }
        Permission permission = new Permission();
        BeanUtils.copyProperties(permissionUpdateVo,permission);
        LambdaUpdateWrapper<Permission> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Permission::getId,permissionUpdateVo.getId());
        this.update(permission,updateWrapper);
    }

    @Override
    public List<PermissionInfoVo> getPermissionList(Integer parentId) {
        return permissionMapper.getPermissionList(parentId);
    }
}




